//serverUrl = 'http://www.happyhelphelp.com/';
serverUrl = 'http://www.huosgo.com/';

moxieApiKey = '03dd45b87d394fe18c268371e21600c1';
//首页全部产品
//app/allProducts

function postServer(route, datas, successfn, errorfn) {
	plus.nativeUI.showWaiting()
	console.log("请求地址" + serverUrl + route + "和参数" + JSON.stringify(datas));
	$.ajax(serverUrl + route, {
		data: datas,
		crossDomain: true,
		dataType: 'json', //服务器返回json格式数据
		type: 'POST', //HTTP请求类型
		timeout: 1000000, //超时时间设置为10秒；
		headers: {
			//			'Content-type': 'application/json',
			jsonType: 'jsonType',
			token: localStorage.getItem('token'),
			smstoken: localStorage.getItem('smstoken')
		},
		success: function(data, status, request) {
			plus.nativeUI.closeWaiting()
			console.log("请求成功地址" + serverUrl + route + "和参数" + JSON.stringify(data));
			if(data.success) {
				successfn(data);
			} else if(!data.success && data.data.code == "1039") {
				//token过期
				localStorage.removeItem('token')
				localStorage.removeItem('lastTime')
				openurl('../landing/login.html')
			} else if(data.data.code == '2000') {
				errorfn(data);
			} else {
				errorfn(data);
			}
			if(request.getResponseHeader("token") != null) {
				localStorage.setItem('token', request.getResponseHeader("token"));
				localStorage.setItem('lastTime', Date.parse(new Date()));
			}
			if(request.getResponseHeader("smstoken") != null) {
				localStorage.setItem('smstoken', request.getResponseHeader("smstoken"))
			}

		},
		error: function(xhr, type, errorThrown) {
			console.log("请求失败地址" + serverUrl + route + "和参数" + JSON.stringify(datas));
			console.log(xhr.status + "   " + errorThrown)
			mui.toast('请求失败!' + type);
		}
	});
}

function getServer(route, datas, successfn, errorfn) {
	console.log("请求地址" + serverUrl + route + "和参数" + JSON.stringify(datas));
	$.ajax(serverUrl + route, {
		data: datas,
		crossDomain: true,
		dataType: 'json', //服务器返回json格式数据
		type: 'GET', //HTTP请求类型
		timeout: 1000000, //超时时间设置为10秒；
		headers: {
			jsonType: 'jsonType',
			token: localStorage.getItem('token'),
			smstoken: ''
		},
		success: function(data, status, request) {
			console.log("请求成功地址" + serverUrl + route + "和参数" + JSON.stringify(data));
			if(data.success) {
				successfn(data);
			} else if(!data.success && data.data.code == "1039") {
				//token过期
				localStorage.removeItem('token')
				localStorage.removeItem('lastTime')
				openurl('../landing/login.html')
			} else if(data.data.code == '2000') {
				errorfn(data);
			} else {
				errorfn(data);
			}
			//			console.log(request.getResponseHeader("smstoken"))
			//			console.log(request.getResponseHeader("token"))
			if(request.getResponseHeader("token") != null) {
				localStorage.setItem('token', request.getResponseHeader("token"))
				localStorage.setItem('lastTime', Date.parse(new Date()));
			}
			if(request.getResponseHeader("smstoken") != null) {
				localStorage.setItem('smstoken', request.getResponseHeader("smstoken"))
			}
		},
		error: function(xhr, type, errorThrown) {
			console.log("请求失败地址" + serverUrl + route + "和参数" + JSON.stringify(datas));
			mui.toast('请求失败!' + type);
		}
	});
}

//页面跳转
function openurl(a, parms) {
	mui.openWindow({
		id: a,
		url: a,
		show: {
			aniShow: 'pop-in',
			duration: 250
		},
		extras: { //extras里面的就是参数了
			'parms': parms //tq: weather,
		},
		waiting: {
			autoShow: false, //自动显示等待框，默认为true
		}
	});
}

//判断时间间隔
function judgeTimeDiffer() {
	getServer('app/member/account', "", function(res) {})
	//	var lastTime = new Date() ;
	var lastTime = localStorage.getItem('lastTime');
	//console.log(localStorage.getItem('lastTime'))
	if(localStorage.getItem('lastTime') == null) {
		return false;
	} else {
		//		console.log("lasttime" + lastTime)
		var time = new Date() - parseInt(localStorage.getItem('lastTime'));
		console.log('time' + time);
		if((time / 1000 / 60) > 10) {
			return false
		} else {
			return true;
		}

	}

}
//接口验证
//1身份证四要素
//2银行卡四要素
//4运营商四要素
//8京东认证
//16淘宝认证
//32支付宝认证
//64社保认证
//128公积金认证
//256联系人认证
//512工作认证
//function checkAuth(flag) {
//	var par = {
//		authFlag: flag
//	}
//
//	var p = new Promise(function(resolve, reject) { //做一些异步操作
//		postServer('/app/checkAuth', $.param(par), function(res) {
//			console.log(JSON.stringify(res))
//			//				console.log(res.data.code)
//			if(res.data.step == 0) {
//				checkAuth(flag)
//			} else if(res.data.step == 1) {
//				resolve(data);
//			} else if(res.data.step == -1) {
//				resolve(data);
//				//			error(res)
//				//			success(res)
//				
//			}
//
//		}, function(res) {
//			error(res)
//		});
//	});
//	return p;
//}
function checkAuth(flag) {
	var par = {
		authFlag: flag
	}
	postServer('/app/checkAuth', $.param(par), function(res) {
		console.log(JSON.stringify(res))
		//				console.log(res.data.code)
		if(res.data.step == 0){
			checkAuth(flag)
		}else if(res.data.step == 1){
			checkSuccess(res)
		}else if(res.data.step == -1){
//			error(res)
			checkError(res)
		}else{
			mui.toast(res.message)
			plus.webview.getLaunchWebview().evalJS("activeModel()");
			plus.webview.close(plus.webview.currentWebview());
		}
		
	},function(res){
		checkError(res)
	});
}

// 成功回调
function checkSuccess(res){
	plus.nativeUI.closeWaiting()
	mui.toast("绑定成功");
	plus.webview.getLaunchWebview().evalJS("activeModel()");
	plus.webview.close(plus.webview.currentWebview());
}

// 失败回调
function checkError(res){
	plus.nativeUI.closeWaiting()
	mui.toast(res.data.result);
}


function invokeSettime(obj) {
	var countdown = 60;
	settime(obj);

	function settime(obj) {
		if(countdown == 0) {
			$(obj).attr("disabled", false);
			$(obj).text("获取验证码");
			countdown = 60;
			return;
		} else {
			$(obj).attr("disabled", true);
			$(obj).text("(" + countdown + ") s 重新发送");
			countdown--;
		}
		setTimeout(function() {
			settime(obj)

		}, 1000)
	}
}